#include <amxmodx>
#include <amxmisc>
#include <fun>

#define VERSION	"0.2"

#define CVAR_SPAWN_DMG 	"spawn_damage"
#define CVAR_SPAWN_TIME	"spawn_damage_time"

new g_iMsgScreenFade;
new g_iMsgDamage;
new gCVAR_SPAWN_DMG;
new gCVAR_SPAWN_TIME;

#define DMG_BULLET (1<<1)

public plugin_init()
{
	register_plugin("Spawn Mirror Damage", VERSION, "v3x");

	register_logevent("Logevent_RoundStart", 2, "0=World triggered", "1=Round_Start");
	register_event("Damage", "Event_Damage", "b", "2>0");

	gCVAR_SPAWN_DMG = register_cvar(CVAR_SPAWN_DMG, "1");
	gCVAR_SPAWN_TIME = register_cvar(CVAR_SPAWN_TIME,"5.0");

	g_iMsgScreenFade = get_user_msgid("ScreenFade");
	g_iMsgDamage = get_user_msgid("Damage");
}

new bool:g_bCanShoot;

public Logevent_RoundStart()
{
	if (get_cvar_num(CVAR_SPAWN_DMG))
	{
		g_bCanShoot = false;
		set_task(get_pcvar_float(gCVAR_SPAWN_TIME), "CanShoot", 21714);

		new szMsgStr[64];
		new sec[2];
		new s = get_pcvar_num(gCVAR_SPAWN_TIME);
		if(s == 1)
			sec[0] = 'e';
		else if(s == 2 && s == 3)
			sec[0] = 'y';
		format(szMsgStr, 63, "*** Zakaz atakowania przez %i sekund%s ***", get_pcvar_num(gCVAR_SPAWN_TIME),sec);
		
		set_hudmessage(255, 25, 25, -1.0, -1.0, _, _, 2.0, _, _, 4);
		show_hudmessage(0, szMsgStr);
	}
}

public CanShoot()
{
  g_bCanShoot = true;
}

public Event_Damage( id )
{
	if (get_cvar_num(CVAR_SPAWN_DMG) && !g_bCanShoot)
	{
		new iAttacker = get_user_attacker(id);
		new iHealth = get_user_health(id);
		new iDamage = read_data(2);

		if(!is_user_connected(iAttacker) || !is_user_connected(id) || iAttacker == id)
			return;
		
		if (iHealth - iDamage <= 0)
		{
			user_kill(iAttacker);

			new szUserName[33];
			get_user_name(iAttacker, szUserName, 32);

			client_print(0, print_chat, "* %s zostal zabity za spawnkill", szUserName);
		}
		else if (iHealth - iDamage > 0)
		{
			client_print(iAttacker, print_chat, "* Nie atakuj w czasie spawn'a");
			set_user_health(iAttacker, iHealth - iDamage);
		}

		message_begin( MSG_ONE_UNRELIABLE, g_iMsgScreenFade, {0,0,0}, iAttacker );
		write_short( 1<<10 );
		write_short( 1<<10 );
		write_short( 1<<12 );
		write_byte( 200 );
		write_byte( 0 );
		write_byte( 0 );
		write_byte( 50 );
		message_end();

		new iOrigin[3];
		get_user_origin(iAttacker, iOrigin, 0);

		message_begin(MSG_ONE, g_iMsgDamage, {0,0,0}, iAttacker);
		write_byte( 21 );
		write_byte( 20 );
		write_long( DMG_BULLET );
		write_coord( iOrigin[0] );
		write_coord( iOrigin[1] );
		write_coord( iOrigin[2] );
		message_end();
	}
}
